package 刷题10月.每日一题0930;
// https://leetcode-cn.com/problems/number-complement/
public class 位运算476数字的补数1018 {
    class Solution {
        public int findComplement(int num) {
            // 找到最高位的1，左移一位并-1，异或
            int highbit = 1;
            int x = num;
            while (x != 0) {
                // 找最低位（最右边）的 1
                highbit = x & (-x);
                // 打掉最低位（最右边）的1
                x = x & (x - 1);
            }

            return num ^ ((highbit << 1) - 1);
        }
    }

}
